package com.yygo.util;

import com.cloopen.rest.sdk.CCPRestSDK;

import java.util.HashMap;
import java.util.Set;

/**
 * Created by tiansha on 2015/11/26.
 */
public class SMSUtil {

    public final static String CONFIRM_COVER_TEMPLATE = "51788";
    public final static String UPLOAD_COVER_TEMPLATE = "52065";
    public final static String CONFIRM_POLICY_TEMPLATE = "51789";

    public static void sendConfirmCoverMessage(String mobileNo, String number, String customer, String amount) {
        CCPRestSDK restAPI = initAPI();

        HashMap<String, Object> result = null;
        result = restAPI.sendTemplateSMS(mobileNo, CONFIRM_COVER_TEMPLATE, new String[]{number, customer, amount});

        checkResult(result);
    }

    public static void sendUploadCoverMessage(String mobileNo, String number, String customer, String amount) {
        CCPRestSDK restAPI = initAPI();

        HashMap<String, Object> result = null;
        result = restAPI.sendTemplateSMS(mobileNo, UPLOAD_COVER_TEMPLATE, new String[]{number, customer, amount});

        checkResult(result);
    }

    public static void sendConfirmPolicyMessage(String mobileNo, String number, String customer, String amount, String date) {
        CCPRestSDK restAPI = initAPI();

        HashMap<String, Object> result = null;
        result = restAPI.sendTemplateSMS(mobileNo, CONFIRM_POLICY_TEMPLATE, new String[]{number, customer, amount, date});

        checkResult(result);
    }

    private static CCPRestSDK initAPI() {
        CCPRestSDK restAPI = new CCPRestSDK();
        restAPI.init("app.cloopen.com", "8883");// 初始化服务器地址和端口，格式如下，服务器地址不需要写https://
        restAPI.setAccount("8a48b551506fd26f0150883dfb284185", "337b6894eccb4ec1abd613ca665cc598");// 初始化主帐号和主帐号TOKEN
        restAPI.setAppId("8a48b5515124598801513394c6cd2f2c");// 初始化应用ID
        return restAPI;
    }

    private static void checkResult(HashMap<String, Object> result) {
        System.out.println("SDKTestSendTemplateSMS result=" + result);
        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
        }
    }
}
